﻿using System.Drawing;
using System.Drawing.Drawing2D;
using FBEntities.FBObjects;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using XDAFacebook;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemEvent : XFItemBase
    {
        private readonly FBEvent _event;
        private readonly Font _nameFont;
        private int _height;
        private int _lineSpace = 5;
        private int _pictureWidth = Settings.IconSize;

        public XFItemEvent(FBEvent @event)
        {
            _event = @event;

            Style = new XFItemStyle
                        {
                            BackgroundColor = Color.White,
                            TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                            TextColor = Color.Black,
                            SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7.8f, FontStyle.Underline),
                            SecondaryTextColor = Color.Navy,
                            DashStyleBottom = DashStyle.Solid,
                            BorderBottomColor = Color.DarkGray,
                            Padding = 5
                        };

            _nameFont = new Font(Style.TextFont.Name, Style.TextFont.Size + .2f, FontStyle.Bold);

            ItemType = XFItemType.Display;
        }

        public override int GetHeight()
        {
            if (_height > 0)
                return _height;

            using (var b = XFBitmapFactory.GetBitmap(1, 1))
            using (Graphics g = Graphics.FromImage(b))
                _height = paintEvent(g);

            return _height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            paintEvent(g);
        }

        private int paintEvent(Graphics g)
        {
            int width = GetWidth();
            int currX = Style.PaddingLeft + _pictureWidth + Style.PaddingRight;
            int currY = Style.PaddingTop;

            int currWidth = width - (currX + Style.PaddingRight + Style.PaddingLeft);

            Color textColor = getCurrentFontColor(false);

            drawText(g, _event.name, ref currX, ref currWidth, ref currY, _nameFont, textColor);
            currY += _lineSpace;

            drawText(g, Settings.LocalizationStrings.Location + ": " + _event.location, ref currX, ref currWidth, ref currY, Style.TextFont, textColor);
            drawText(g, Settings.LocalizationStrings.StartTime + ": " + _event.start_time.ToShortDateString(), ref currX, ref currWidth, ref currY,
                     Style.TextFont, Color.DarkGray);
            drawText(g, Settings.LocalizationStrings.EndTime + ": " + _event.end_time.ToShortDateString(), ref currX, ref currWidth, ref currY,
                     Style.TextFont, Color.DarkGray);

            currY += _lineSpace;
            drawText(g, Settings.LocalizationStrings.RSVPStatus + ": " + _event.rsvp_status, ref currX, ref currWidth, ref currY,
                     Style.SecondaryTextFont, Color.Black);

            return currY;
        }

        private void drawText(Graphics g, string text, ref int currX, ref int currWidth, ref int currY, Font font,
                              Color color)
        {
            if (!string.IsNullOrEmpty(text))
            {
                int h = g.DrawString(text, new Rectangle(currX, currY, currWidth, 0), Style, font, color) + _lineSpace;
                currY += h;
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            _nameFont.Dispose();
        }
    }
}